﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.UserControls;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_TOOL.Tool.Forms
{
	public partial class frm_FrequencyAdd : DevExpress.XtraEditors.XtraForm
	{
		private ucFrequency parent;
        private LogUtils log;
        public frm_FrequencyAdd(ucFrequency frmFrequency)
		{
            InitializeComponent();
            btAdd.Image = ERP_RORZE_TOOL.Properties.Resources.add;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
			parent = frmFrequency;
			this.CancelButton = this.btCancel;
            log = new LogUtils();
			
		}

		private void btAdd_Click(object sender, EventArgs e)
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			if (GetData(helper.ValueObject))
			{
				if (helper.Insert() > 0)
				{
                    //write Log
                    log.Table = LogConstant.Table.ToolCutting.Frequency;
                    log.Task = LogConstant.Task.Insert;
                    log.Description = helper.ValueObject.FrequencyID;
                    log.WriteLog();

                    if (Utilities.MessageConfirmationYesNo(ToolConstants.Message.AddSuccessAndQuestion))
				    {

				    txtFrequencyID.Text = "";
				    txtFrequencyName.Text = "";
				    txtFrequencyNameEnglish.Text = "";
				    txtFrequencyID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }

				}
				else
				{
                    Utilities.MessageErrorOk(ToolConstants.Message.AddFailed);
				}
				parent._action = 0;
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
		#region Private methods

		private bool CheckDuplicate(string frequencyId)
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			helper.ValueObject.FrequencyID = frequencyId;
			DataTable dt = helper.GetOne();
			if(dt.Rows != null && dt.Rows.Count > 0)
				return true;
			return false;

		}
		private bool GetData(Frequency o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtFrequencyID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Frequency.FrequencyIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtFrequencyName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Frequency.FrequencyNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			if(CheckDuplicate(txtFrequencyID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Frequency.FrequencyHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			
			#endregion

			o.FrequencyID = txtFrequencyID.Text;
			o.FrequencyName = txtFrequencyName.Text;
			o.FrequencyName_English = txtFrequencyNameEnglish.Text;

			return true;
		}
		
		#endregion
	}
}